/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-10 10:34:48
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-10 10:30:38
 */

import {
  orderAPI
} from "../../../../../api/index"
import {
  returnStatus,
  returnStatusToast
} from '../../../../../utils/util'


// pages/mine/order/afterOrder/action/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    afterOrderInfo: {
      'aftersaleId': {
        title: '售后编号：',
        value: '1000000892'
      },
      'aftersaleTypeName': {
        title: '售后类型：',
        value: '既退货，也退款'
      },
      'aftersaleWay': {
        title: '退款方式：',
        value: '原路退回'
      },
      'updateDate':{
        title:'退款时间',
        value:''
      },
      'refundAmount': {
        title: '可退金额：',
        value: '¥168',
        red: true
      }
    },
    showPostSwitch: false,
    postSwitch: [{
      title: '顺丰快递',
      value: 0
    }],
    show_afterAddress: false,
    address:{},
    form:{
      logisticsCompany:''
    },
    read: false
  },
  hideShowAfterAddress() {
    this.setData({
      show_afterAddress: false
    })
  },
  openShowAfterAddress() {
    this.setData({
      show_afterAddress: true
    })
  },
  showExpress(){
    this.setData({showPostSwitch:true})
  },
  hideExpress(){
    this.setData({showPostSwitch:false})
  },

  onChangeExpress({detail}){
    this.setData({
      'form.logisticsCompany':detail
    })
  },
  onInputChange({detail}){
    this.setData({
      'form.logisticsCode': detail
    })
  },
  onSwitchExpress({currentTarget:{dataset:{name}}}){
    this.setData({
      'form.logisticsCompany':name
    })
  },
  // 提交售后信息
  onSubmit(){
    const form = this.data.form
    if(!form.logisticsCode){
      wx.showToast({
        title: '请填写快递单号',
        icon: 'none',
      });
      return false
    }else if(!form.logisticsCompany){
      wx.showToast({
        title: '请选择快递公司',
        icon: 'none',
      })
      return false
    }
    orderAPI.sendBackGoods(this.data.form).then(async res=>{
      returnStatusToast(res,()=>{
        wx.redirectTo({
          url: `/pages/mine/order/afterOrder/status?status=false&message=退货运单号已经提交，正在等待仓库收货，验收无误后财务将原路退回售后金额，请耐心等待…`,
          success: (result)=>{
            
          },
          fail: ()=>{},
          complete: ()=>{}
        });
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var appInst =  getApp();
    appInst.setTitle('售后')
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('params',async  ({info,info:{aftersaleId},read})=>{
      if (info) {
        if(!info.updateDate){
          // 请求售后详情
          const _ = await orderAPI.queryAfterSaleInfo({aftersaleId})
          info = await returnStatus(_)
        }
        const orderInfo = this.data.afterOrderInfo
        if(!read){
          delete orderInfo.updateDate
        }
        for (const key in orderInfo) {
          const temp = `afterOrderInfo.${key}.value`
          this.setData({
            [temp]: (key=='refundAmount'?'¥'+info[key]:info[key]) || orderInfo[key].value
          })
        }
        this.setData({
          info,
          read,
          'form.afterSaleId':info.aftersaleId
        })
      }
      if(read && info.updateDate) return
      // 获取快递公司
      orderAPI.queryExpress().then(async res=>{
        const postSwitch = await returnStatus(res)
        if(postSwitch){
          this.setData({postSwitch:postSwitch.expresses})
        }
      })
      // 获取售后地址
      orderAPI.queryAfterSafeAddress({aftersaleId}).then(async res=>{
        const address = await returnStatus(res)
        this.setData({address})
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})